package com.example.demo.web;

import com.example.demo.web.base.BaseResp;
import com.example.demo.web.base.ErrorMap;
import com.example.demo.web.base.JsonRetException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.entity.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiabin on 2017/7/17.
 */
@ControllerAdvice
public class ExceptionController {
    public static final String DEFAULT_ERROR_VIEW = "error";
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        return mav;
    }

    @ExceptionHandler(value = JsonRetException.class)
    @ResponseBody
    public BaseResp<Null> APIErrorHandder(HttpServletRequest req, JsonRetException e) throws Exception {
        return new BaseResp<>(e.getLocalmsg().getErrorCode(), e.getLocalmsg().getErrorMsg(), null);
    }
}
